"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "FieldCode", {
  enumerable: true,
  get: function get() {
    return _Code.default;
  }
});
Object.defineProperty(exports, "FieldDatePicker", {
  enumerable: true,
  get: function get() {
    return _DatePicker.default;
  }
});
Object.defineProperty(exports, "FieldIndexColumn", {
  enumerable: true,
  get: function get() {
    return _IndexColumn.default;
  }
});
Object.defineProperty(exports, "FieldMoney", {
  enumerable: true,
  get: function get() {
    return _Money.default;
  }
});
Object.defineProperty(exports, "FieldPercent", {
  enumerable: true,
  get: function get() {
    return _Percent.default;
  }
});
Object.defineProperty(exports, "FieldProgress", {
  enumerable: true,
  get: function get() {
    return _Progress.default;
  }
});
Object.defineProperty(exports, "FieldRangePicker", {
  enumerable: true,
  get: function get() {
    return _RangePicker.default;
  }
});
Object.defineProperty(exports, "FieldSelect", {
  enumerable: true,
  get: function get() {
    return _Select.default;
  }
});
Object.defineProperty(exports, "FieldStatus", {
  enumerable: true,
  get: function get() {
    return _Status.default;
  }
});
Object.defineProperty(exports, "FieldText", {
  enumerable: true,
  get: function get() {
    return _Text.default;
  }
});
Object.defineProperty(exports, "FieldTimePicker", {
  enumerable: true,
  get: function get() {
    return _TimePicker.default;
  }
});
exports.defaultRenderText = exports.default = void 0;
Object.defineProperty(exports, "proFieldParsingText", {
  enumerable: true,
  get: function get() {
    return _Select.proFieldParsingText;
  }
});
Object.defineProperty(exports, "proFieldParsingValueEnumToArray", {
  enumerable: true,
  get: function get() {
    return _Select.proFieldParsingValueEnumToArray;
  }
});

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _jsxRuntime = require("react/jsx-runtime");

var _proProvider = _interopRequireWildcard(require("@ant-design/pro-provider"));

var _proUtils = require("@ant-design/pro-utils");

var _antdV = require("antd-v4");

var _warning = require("rc-util/lib/warning");

var _react = _interopRequireWildcard(require("react"));

var _Cascader = _interopRequireDefault(require("./components/Cascader"));

var _Checkbox = _interopRequireDefault(require("./components/Checkbox"));

var _Code = _interopRequireDefault(require("./components/Code"));

var _ColorPicker = _interopRequireDefault(require("./components/ColorPicker"));

var _DatePicker = _interopRequireDefault(require("./components/DatePicker"));

var _Digit = _interopRequireDefault(require("./components/Digit"));

var _DigitRange = _interopRequireDefault(require("./components/DigitRange"));

var _FromNow = _interopRequireDefault(require("./components/FromNow"));

var _Image = _interopRequireDefault(require("./components/Image"));

var _IndexColumn = _interopRequireDefault(require("./components/IndexColumn"));

var _Money = _interopRequireDefault(require("./components/Money"));

var _Options = _interopRequireDefault(require("./components/Options"));

var _Password = _interopRequireDefault(require("./components/Password"));

var _Percent = _interopRequireDefault(require("./components/Percent"));

var _Progress = _interopRequireDefault(require("./components/Progress"));

var _Radio = _interopRequireDefault(require("./components/Radio"));

var _RangePicker = _interopRequireDefault(require("./components/RangePicker"));

var _Rate = _interopRequireDefault(require("./components/Rate"));

var _Second = _interopRequireDefault(require("./components/Second"));

var _Select = _interopRequireWildcard(require("./components/Select"));

var _Status = _interopRequireDefault(require("./components/Status"));

var _Switch = _interopRequireDefault(require("./components/Switch"));

var _Text = _interopRequireDefault(require("./components/Text"));

var _TextArea = _interopRequireDefault(require("./components/TextArea"));

var _TimePicker = _interopRequireWildcard(require("./components/TimePicker"));

var _TreeSelect = _interopRequireDefault(require("./components/TreeSelect"));

var _FieldHOC = _interopRequireDefault(require("./FieldHOC"));

var _excluded = ["text", "valueType", "mode", "onChange", "renderFormItem", "value", "readonly"];
var REQUEST_VALUE_TYPE = ['select', 'radio', 'radioButton', 'checkbook'];
/**
 * Render valueType object
 *
 * @param text String | number
 * @param valueType ProColumnsValueObjectType
 */

var defaultRenderTextByObject = function defaultRenderTextByObject(text, valueType, props) {
  var pickFormItemProps = (0, _proUtils.pickProProps)(props.fieldProps);

  if (valueType.type === 'progress') {
    return (0, _jsxRuntime.jsx)(_Progress.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, {
      text: text,
      fieldProps: (0, _objectSpread2.default)({
        status: valueType.status ? valueType.status : undefined
      }, pickFormItemProps)
    }));
  }

  if (valueType.type === 'money') {
    return (0, _jsxRuntime.jsx)(_Money.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({
      locale: valueType.locale
    }, props), {}, {
      fieldProps: pickFormItemProps,
      text: text,
      moneySymbol: valueType.moneySymbol
    }));
  }

  if (valueType.type === 'percent') {
    return (0, _jsxRuntime.jsx)(_Percent.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, {
      text: text,
      showSymbol: valueType.showSymbol,
      precision: valueType.precision,
      fieldProps: pickFormItemProps,
      showColor: valueType.showColor
    }));
  }

  if (valueType.type === 'image') {
    return (0, _jsxRuntime.jsx)(_Image.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, {
      text: text,
      width: valueType.width
    }));
  }

  return text;
};
/**
 * 根据不同的类型来转化数值
 *
 * @param dataValue
 * @param valueType
 */


var defaultRenderText = function defaultRenderText(dataValue, valueType, props, valueTypeMap) {
  var _props$fieldProps;

  var _props$mode = props.mode,
      mode = _props$mode === void 0 ? 'read' : _props$mode,
      _props$emptyText = props.emptyText,
      emptyText = _props$emptyText === void 0 ? '-' : _props$emptyText;

  if (emptyText !== false && mode === 'read' && valueType !== 'option' && valueType !== 'switch') {
    if (typeof dataValue !== 'boolean' && typeof dataValue !== 'number' && !dataValue) {
      var fieldProps = props.fieldProps,
          render = props.render;

      if (render) {
        return render(dataValue, (0, _objectSpread2.default)({
          mode: mode
        }, fieldProps), (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
          children: emptyText
        }));
      }

      return (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
        children: emptyText
      });
    }
  } // eslint-disable-next-line no-param-reassign


  delete props.emptyText;

  if ((0, _typeof2.default)(valueType) === 'object') {
    return defaultRenderTextByObject(dataValue, valueType, props);
  }

  var customValueTypeConfig = valueTypeMap && valueTypeMap[valueType];

  if (customValueTypeConfig) {
    // eslint-disable-next-line no-param-reassign
    delete props.ref;

    if (mode === 'read') {
      var _customValueTypeConfi;

      return (_customValueTypeConfi = customValueTypeConfig.render) === null || _customValueTypeConfi === void 0 ? void 0 : _customValueTypeConfi.call(customValueTypeConfig, dataValue, (0, _objectSpread2.default)((0, _objectSpread2.default)({
        text: dataValue
      }, props), {}, {
        mode: mode || 'read'
      }), (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
        children: dataValue
      }));
    }

    if (mode === 'update' || mode === 'edit') {
      var _customValueTypeConfi2;

      return (_customValueTypeConfi2 = customValueTypeConfig.renderFormItem) === null || _customValueTypeConfi2 === void 0 ? void 0 : _customValueTypeConfi2.call(customValueTypeConfig, dataValue, (0, _objectSpread2.default)({
        text: dataValue
      }, props), (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
        children: dataValue
      }));
    }
  }

  var needValueEnum = REQUEST_VALUE_TYPE.includes(valueType);
  var hasValueEnum = !!(props.valueEnum || props.request || props.options || ((_props$fieldProps = props.fieldProps) === null || _props$fieldProps === void 0 ? void 0 : _props$fieldProps.options));
  (0, _warning.noteOnce)(!needValueEnum || hasValueEnum, "\u5982\u679C\u8BBE\u7F6E\u4E86 valueType \u4E3A ".concat(REQUEST_VALUE_TYPE.join(','), "\u4E2D\u4EFB\u610F\u4E00\u4E2A\uFF0C\u5219\u9700\u8981\u914D\u7F6Eoptions\uFF0Crequest, valueEnum \u5176\u4E2D\u4E4B\u4E00\uFF0C\u5426\u5219\u65E0\u6CD5\u751F\u6210\u9009\u9879\u3002"));
  (0, _warning.noteOnce)(!needValueEnum || hasValueEnum, "If you set valueType to any of ".concat(REQUEST_VALUE_TYPE.join(','), ", you need to configure options, request or valueEnum."));
  /** 如果是金额的值 */

  if (valueType === 'money') {
    return (0, _jsxRuntime.jsx)(_Money.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, {
      text: dataValue
    }));
  }
  /** 如果是日期的值 */


  if (valueType === 'date') {
    return (0, _jsxRuntime.jsx)(_FieldHOC.default, {
      isLight: props.light,
      children: (0, _jsxRuntime.jsx)(_DatePicker.default, (0, _objectSpread2.default)({
        text: dataValue,
        format: "YYYY-MM-DD"
      }, props))
    });
  }
  /** 如果是周的值 */


  if (valueType === 'dateWeek') {
    return (0, _jsxRuntime.jsx)(_FieldHOC.default, {
      isLight: props.light,
      children: (0, _jsxRuntime.jsx)(_DatePicker.default, (0, _objectSpread2.default)({
        text: dataValue,
        format: "YYYY-wo",
        picker: "week"
      }, props))
    });
  }
  /** 如果是月的值 */


  if (valueType === 'dateMonth') {
    return (0, _jsxRuntime.jsx)(_FieldHOC.default, {
      isLight: props.light,
      children: (0, _jsxRuntime.jsx)(_DatePicker.default, (0, _objectSpread2.default)({
        text: dataValue,
        format: "YYYY-MM",
        picker: "month"
      }, props))
    });
  }
  /** 如果是季度的值 */


  if (valueType === 'dateQuarter') {
    return (0, _jsxRuntime.jsx)(_FieldHOC.default, {
      isLight: props.light,
      children: (0, _jsxRuntime.jsx)(_DatePicker.default, (0, _objectSpread2.default)({
        text: dataValue,
        format: "YYYY-\\QQ",
        picker: "quarter"
      }, props))
    });
  }
  /** 如果是年的值 */


  if (valueType === 'dateYear') {
    return (0, _jsxRuntime.jsx)(_FieldHOC.default, {
      isLight: props.light,
      children: (0, _jsxRuntime.jsx)(_DatePicker.default, (0, _objectSpread2.default)({
        text: dataValue,
        format: "YYYY",
        picker: "year"
      }, props))
    });
  }
  /** 如果是日期范围的值 */


  if (valueType === 'dateRange') {
    return (0, _jsxRuntime.jsx)(_RangePicker.default, (0, _objectSpread2.default)({
      text: dataValue,
      format: "YYYY-MM-DD"
    }, props));
  }
  /** 如果是日期加时间类型的值 */


  if (valueType === 'dateTime') {
    return (0, _jsxRuntime.jsx)(_FieldHOC.default, {
      isLight: props.light,
      children: (0, _jsxRuntime.jsx)(_DatePicker.default, (0, _objectSpread2.default)({
        text: dataValue,
        format: "YYYY-MM-DD HH:mm:ss",
        showTime: true
      }, props))
    });
  }
  /** 如果是日期加时间类型的值的值 */


  if (valueType === 'dateTimeRange') {
    // 值不存在的时候显示 "-"
    return (0, _jsxRuntime.jsx)(_FieldHOC.default, {
      isLight: props.light,
      children: (0, _jsxRuntime.jsx)(_RangePicker.default, (0, _objectSpread2.default)({
        text: dataValue,
        format: "YYYY-MM-DD HH:mm:ss",
        showTime: true
      }, props))
    });
  }
  /** 如果是时间类型的值 */


  if (valueType === 'time') {
    return (0, _jsxRuntime.jsx)(_FieldHOC.default, {
      isLight: props.light,
      children: (0, _jsxRuntime.jsx)(_TimePicker.default, (0, _objectSpread2.default)({
        text: dataValue,
        format: "HH:mm:ss"
      }, props))
    });
  }
  /** 如果是时间类型的值 */


  if (valueType === 'timeRange') {
    return (0, _jsxRuntime.jsx)(_FieldHOC.default, {
      isLight: props.light,
      children: (0, _jsxRuntime.jsx)(_TimePicker.FieldTimeRangePicker, (0, _objectSpread2.default)({
        text: dataValue,
        format: "HH:mm:ss"
      }, props))
    });
  }

  if (valueType === 'fromNow') {
    return (0, _jsxRuntime.jsx)(_FromNow.default, (0, _objectSpread2.default)({
      text: dataValue
    }, props));
  }

  if (valueType === 'index') {
    return (0, _jsxRuntime.jsx)(_IndexColumn.default, {
      children: dataValue + 1
    });
  }

  if (valueType === 'indexBorder') {
    return (0, _jsxRuntime.jsx)(_IndexColumn.default, {
      border: true,
      children: dataValue + 1
    });
  }

  if (valueType === 'progress') {
    return (0, _jsxRuntime.jsx)(_Progress.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, {
      text: dataValue
    }));
  }
  /** 百分比, 默认展示符号, 不展示小数位 */


  if (valueType === 'percent') {
    return (0, _jsxRuntime.jsx)(_Percent.default, (0, _objectSpread2.default)({
      text: dataValue
    }, props));
  }

  if (valueType === 'avatar' && typeof dataValue === 'string' && props.mode === 'read') {
    return (0, _jsxRuntime.jsx)(_antdV.Avatar, {
      src: dataValue,
      size: 22,
      shape: "circle"
    });
  }

  if (valueType === 'code') {
    return (0, _jsxRuntime.jsx)(_Code.default, (0, _objectSpread2.default)({
      text: dataValue
    }, props));
  }

  if (valueType === 'jsonCode') {
    return (0, _jsxRuntime.jsx)(_Code.default, (0, _objectSpread2.default)({
      text: dataValue,
      language: "json"
    }, props));
  }

  if (valueType === 'textarea') {
    return (0, _jsxRuntime.jsx)(_TextArea.default, (0, _objectSpread2.default)({
      text: dataValue
    }, props));
  }

  if (valueType === 'digit') {
    return (0, _jsxRuntime.jsx)(_Digit.default, (0, _objectSpread2.default)({
      text: dataValue
    }, props));
  }

  if (valueType === 'digitRange') {
    return (0, _jsxRuntime.jsx)(_DigitRange.default, (0, _objectSpread2.default)({
      text: dataValue
    }, props));
  }

  if (valueType === 'second') {
    return (0, _jsxRuntime.jsx)(_Second.default, (0, _objectSpread2.default)({
      text: dataValue
    }, props));
  }

  if (valueType === 'select' || valueType === 'text' && (props.valueEnum || props.request)) {
    return (0, _jsxRuntime.jsx)(_FieldHOC.default, {
      isLight: props.light,
      children: (0, _jsxRuntime.jsx)(_Select.default, (0, _objectSpread2.default)({
        text: dataValue
      }, props))
    });
  }

  if (valueType === 'checkbox') {
    return (0, _jsxRuntime.jsx)(_Checkbox.default, (0, _objectSpread2.default)({
      text: dataValue
    }, props));
  }

  if (valueType === 'radio') {
    return (0, _jsxRuntime.jsx)(_Radio.default, (0, _objectSpread2.default)({
      text: dataValue
    }, props));
  }

  if (valueType === 'radioButton') {
    return (0, _jsxRuntime.jsx)(_Radio.default, (0, _objectSpread2.default)({
      radioType: "button",
      text: dataValue
    }, props));
  }

  if (valueType === 'rate') {
    return (0, _jsxRuntime.jsx)(_Rate.default, (0, _objectSpread2.default)({
      text: dataValue
    }, props));
  }

  if (valueType === 'switch') {
    return (0, _jsxRuntime.jsx)(_Switch.default, (0, _objectSpread2.default)({
      text: dataValue
    }, props));
  }

  if (valueType === 'option') {
    return (0, _jsxRuntime.jsx)(_Options.default, (0, _objectSpread2.default)({
      text: dataValue
    }, props));
  }

  if (valueType === 'password') {
    return (0, _jsxRuntime.jsx)(_Password.default, (0, _objectSpread2.default)({
      text: dataValue
    }, props));
  }

  if (valueType === 'image') {
    return (0, _jsxRuntime.jsx)(_Image.default, (0, _objectSpread2.default)({
      text: dataValue
    }, props));
  }

  if (valueType === 'cascader') {
    return (0, _jsxRuntime.jsx)(_Cascader.default, (0, _objectSpread2.default)({
      text: dataValue
    }, props));
  }

  if (valueType === 'treeSelect') {
    return (0, _jsxRuntime.jsx)(_TreeSelect.default, (0, _objectSpread2.default)({
      text: dataValue
    }, props));
  }

  if (valueType === 'color') {
    return (0, _jsxRuntime.jsx)(_ColorPicker.default, (0, _objectSpread2.default)({
      text: dataValue
    }, props));
  }

  return (0, _jsxRuntime.jsx)(_Text.default, (0, _objectSpread2.default)({
    text: dataValue
  }, props));
};

exports.defaultRenderText = defaultRenderText;

var ProField = function ProField(_ref, ref) {
  var _ref2, _fieldProps$value, _ref3;

  var text = _ref.text,
      _ref$valueType = _ref.valueType,
      valueType = _ref$valueType === void 0 ? 'text' : _ref$valueType,
      _ref$mode = _ref.mode,
      mode = _ref$mode === void 0 ? 'read' : _ref$mode,
      _onChange = _ref.onChange,
      renderFormItem = _ref.renderFormItem,
      value = _ref.value,
      readonly = _ref.readonly,
      rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var intl = (0, _proProvider.useIntl)();
  var context = (0, _react.useContext)(_proProvider.default);
  var fieldProps = (value !== undefined || _onChange || (rest === null || rest === void 0 ? void 0 : rest.fieldProps)) && (0, _objectSpread2.default)((0, _objectSpread2.default)({
    value: value
  }, (0, _proUtils.omitUndefined)(rest === null || rest === void 0 ? void 0 : rest.fieldProps)), {}, {
    onChange: function onChange() {
      var _rest$fieldProps, _rest$fieldProps$onCh;

      for (var _len = arguments.length, restParams = new Array(_len), _key = 0; _key < _len; _key++) {
        restParams[_key] = arguments[_key];
      }

      rest === null || rest === void 0 ? void 0 : (_rest$fieldProps = rest.fieldProps) === null || _rest$fieldProps === void 0 ? void 0 : (_rest$fieldProps$onCh = _rest$fieldProps.onChange) === null || _rest$fieldProps$onCh === void 0 ? void 0 : _rest$fieldProps$onCh.call.apply(_rest$fieldProps$onCh, [_rest$fieldProps].concat(restParams));
      _onChange === null || _onChange === void 0 ? void 0 : _onChange.apply(void 0, restParams);
    }
  });
  return (0, _jsxRuntime.jsx)(_react.default.Fragment, {
    children: defaultRenderText(mode === 'edit' ? (_ref2 = (_fieldProps$value = fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.value) !== null && _fieldProps$value !== void 0 ? _fieldProps$value : text) !== null && _ref2 !== void 0 ? _ref2 : '' : (_ref3 = text !== null && text !== void 0 ? text : fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.value) !== null && _ref3 !== void 0 ? _ref3 : '', valueType || 'text', (0, _objectSpread2.default)((0, _objectSpread2.default)({
      ref: ref
    }, rest), {}, {
      mode: readonly ? 'read' : mode,
      renderFormItem: renderFormItem ? function () {
        var newDom = renderFormItem.apply(void 0, arguments); // renderFormItem 之后的dom可能没有props，这里会帮忙注入一下

        if ( /*#__PURE__*/_react.default.isValidElement(newDom)) return /*#__PURE__*/_react.default.cloneElement(newDom, (0, _objectSpread2.default)((0, _objectSpread2.default)({
          placeholder: rest.placeholder || intl.getMessage('tableForm.inputPlaceholder', '请输入')
        }, fieldProps), newDom.props || {}));
        return newDom;
      } : undefined,
      placeholder: rest.placeholder || intl.getMessage('tableForm.inputPlaceholder', '请输入'),
      fieldProps: (0, _proUtils.pickProProps)(fieldProps)
    }), context.valueTypeMap)
  });
};

var _default = /*#__PURE__*/_react.default.forwardRef(ProField);

exports.default = _default;